package csel.view;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;

import csel.controller.bindings.StringActionCommands;
import csel.model.GameCommand;
import csel.model.Invoker;
import csel.model.Model;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;

import javax.swing.*;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JPanel;

import org.apache.log4j.Logger;

public class Splasher extends Viewport implements ImageObserver, Invoker
{
	private static final Logger logger = Logger.getLogger("csel.view");
	private JFrame frame;
	private SpringLayout layout;
	private ImagePanel bg, panel, logo;
	private JButton newGame, loadGame, quitGame;
	private BufferedImage[] b = new BufferedImage[20];

	private TrampolineListener t = new TrampolineListener();

	public Splasher(Model m) {

		getImages();

		frame = new JFrame("Window un-named due to budget cuts.");
		frame.setSize(650, 775);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		layout = new SpringLayout();
		bg = new ImagePanel(b[8], layout);
		layout.layoutContainer(bg);
		panel = new ImagePanel(b[1], layout);
		logo = new ImagePanel(b[9], layout);

		frame.setContentPane(bg);

		newGame = new JButton(new ImageIcon(b[2]));
		newGame.setContentAreaFilled(false);
		newGame.setBorderPainted(false);
		newGame.setActionCommand(StringActionCommands.SP_NEW);

		loadGame = new JButton(new ImageIcon(b[4]));
		loadGame.setContentAreaFilled(false);
		loadGame.setBorderPainted(false);
		loadGame.setActionCommand(StringActionCommands.SP_LOAD);

		quitGame = new JButton(new ImageIcon(b[6]));
		quitGame.setContentAreaFilled(false);
		quitGame.setBorderPainted(false);
		quitGame.setActionCommand(StringActionCommands.SP_QUIT);

		SpringLayout.Constraints newGameCons = layout.getConstraints(newGame);
		newGameCons.setX(Spring.constant(20));
		newGameCons.setY(Spring.constant(600));

		SpringLayout.Constraints loadGameCons = layout.getConstraints(loadGame);
		loadGameCons.setX(Spring.constant(220));
		loadGameCons.setY(Spring.constant(600));

		SpringLayout.Constraints quitGameCons = layout.getConstraints(quitGame);
		quitGameCons.setX(Spring.constant(430));
		quitGameCons.setY(Spring.constant(600));
		
		SpringLayout.Constraints pressKeyCons = layout.getConstraints(panel);
		pressKeyCons.setX(Spring.constant(200));
		pressKeyCons.setY(Spring.constant(600));
		
		SpringLayout.Constraints theLogoCons = layout.getConstraints(logo);
		theLogoCons.setX(Spring.constant(100));
		theLogoCons.setY(Spring.constant(100));
		
		frame.setResizable(false);
		addButtons();
//		frame.addKeyListener(new PressAnyKeyListener());
	}

	public void init()
	{
		newGame.addActionListener(t);
		loadGame.addActionListener(t);
		quitGame.addActionListener(t);
	}

//	private void pressAnyKey() {
//		bg.add(panel);
//		currentTime = System.nanoTime();
//
//		while (anyKey) {
//			panel.changeImage(b[1]);
//			frame.repaint();
//			currentTime = System.nanoTime();
//			while (System.nanoTime() < currentTime + 300000000l && anyKey) {}
//
//			panel.changeImage(b[0]);
//			frame.repaint();
//			currentTime = System.nanoTime();
//			while (System.nanoTime() < currentTime + 700000000l && anyKey) {}
//
//		}
//		panel.changeImage(b[1]);
//		showButtons();
//	}

	private void addButtons() {
		newGame.setVisible(false);
		loadGame.setVisible(false);
		quitGame.setVisible(false);
		bg.add(newGame);
		bg.add(loadGame);
		bg.add(quitGame);
	}
	
	private void showButtons(){
		newGame.setVisible(true);
		loadGame.setVisible(true);
		quitGame.setVisible(true);

//		SpringLayout.Constraints theLogoCons = layout.getConstraints(logo);
//		theLogoCons.setX(Spring.constant(125));
//		theLogoCons.setY(Spring.constant(100));
		bg.add(logo);
	}

	private void getImages() {
		try {
			b[0] = ImageIO.read(new File("images/press1.png"));    // press any key
			b[1] = ImageIO.read(new File("images/press2.png"));    // press any key blank
			b[2] = ImageIO.read(new File("images/newgame.png"));   // new game not selected
			b[3] = ImageIO.read(new File("images/newgames.png"));  // new game selected
			b[4] = ImageIO.read(new File("images/loadgame.png"));  // load game not selected
			b[5] = ImageIO.read(new File("images/loadgames.png")); // load game selected
			b[6] = ImageIO.read(new File("images/quitgame.png"));  // quit game not selected
			b[7] = ImageIO.read(new File("images/quitgames.png")); // quit game selected
			b[8] = ImageIO.read(new File("images/back.png"));      // background image
			b[9] = ImageIO.read(new File("images/logo.png"));      // logo
		} catch (IOException e) { e.printStackTrace(); }
	}

	/*
	 * // paints the image public void paintComponent(Graphics g) {
	 * super.paintComponent(g); //g.drawImage(image, 0, 0, this); }
	 */
	// makes the screen visible or invisible
	public void setVisible(boolean b)
	{
		logger.debug("Splasher setVisible().");

		frame.setVisible(b);
		showButtons();
//		pressAnyKey();
	}

	// destroys the splash screen, never to be seen again.
	public void close()
	{
		logger.info("Splasher closing...");

		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				frame.dispose();
			}
		});
	}

	@Override
	protected Container getContainer()
	{
		return frame;
	}

	public boolean imageUpdate(Image img, int infoflags, int x, int y,
			int width, int height)
	{
		// TODO Auto-generated method stub
		return false;
	}

	public void accept(GameCommand gameCommand)
	{
		logger.debug(String.format("Splasher got command: %s", gameCommand));

		gameCommand.execute();
	}

	private class ImagePanel extends JPanel
	{

		private static final long serialVersionUID = 1L;
		private Image img;

		public ImagePanel(String img)
		{
			this(new ImageIcon(img).getImage());
		}

		public ImagePanel(Image img)
		{
			this.img = img;
			Dimension size = new Dimension(img.getWidth(null), img
					.getHeight(null));
			setPreferredSize(size);
			setMinimumSize(size);
			setMaximumSize(size);
			setSize(size);
			setLayout(null);
		}

		public ImagePanel(Image img, SpringLayout layout)
		{
			this.img = img;
			Dimension size = new Dimension(img.getWidth(null), img
					.getHeight(null));
			setPreferredSize(size);
			setMinimumSize(size);
			setMaximumSize(size);
			setSize(size);
			setLayout(layout);
		}

		public void changeImage(Image img)
		{
			this.img = img;
		}

		public void paintComponent(Graphics g)
		{
			g.drawImage(img, 0, 0, null);
		}

	}
}
